<?php
namespace App\Controller\Admin;

use App\Model\Admin;
use App\Query\AdminQuery;
use App\Repository\AdminRepository;
use App\Request\{
    AdminRequest,
    AdminUpdateRequest
};

class AdminController extends Controller
{
    // 列表
    public function index()
    {
        return AdminQuery::model()->get();
    }
    
    // 新增
    public function create(AdminRequest $request)
    {
        if(AdminRepository::make()->create($request->validated()))
        {
            $this->message('create');
        }
        
        $this->error('create');
    }
    
    // 更新
    public function update($id, AdminUpdateRequest $request)
    {
        $admin = AdminRepository::find($id);

        if($admin && $admin->update($request->validated()))
        {
            $this->message('update');
        }
        
        $this->error('update');
    }
    
    // 删除
    public function delete($id)
    {
        $admin = AdminRepository::find($id);
        
        if($admin && $admin->delete())
        {
            $this->message('delete');
        }

        $this->error('delete');
    }
}